# coding:utf-8

import os
import urllib

import set_env_path     # 请勿删除


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from common.im_util import im_client

Base = declarative_base()
engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
Session = sessionmaker(bind=engine)
session = Session()


def apply_im_token_for_user():
    """
    created by chenhj at 2017/8/24
    1. 查询出没有im的用户
    2. 对他们生成im
    """
    select_no_im_sql = """
        select id, nickname, avatar, com, city from user
         where mobile in ('13560199765', '13580293793', '13929569170',
          '15113478687', '18826416361', '13296874281', '13430607063');
    """

    result = session.execute(select_no_im_sql).fetchall()
    for each in result:
        print(each.id)
        host = os.environ.get('img_host')
        avatar = urllib.parse.urljoin(host, each.avatar)
        im_client.update_user_info(
            each.id, each.nickname, avatar, each.com, each.city
        )


if __name__ == '__main__':
    apply_im_token_for_user()

